#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
//	system("start https://www.lanqiao.cn/problems/18210/learning/");

    int n;
    cin >> n;

    char x;
    int y;
    unordered_map<char, int> mp;
    for(int i = 1; i <= n; i++) {
        cin >> x >> y;
        mp[x] = y;
    }

    string s;
    cin >> s;

    int ans = 0;

    unordered_map<char, int> cnt;
    for(auto i : s) {
        cnt[i]++;
        if(cnt[i] == 3) {
            ans += 2 * mp[i];
            cnt[i] = 0;
        }
    }

    for(auto i : cnt)
        ans += mp[i.first] * i.second;

    cout << ans;

    return 0;
}

